package com.rm.zeyou.voice.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.entity.MiddleNotify;
import com.rm.zeyou.entity.VoiceNotify;
import com.rm.zeyou.mapper.MiddleNotifyMapper;
import com.rm.zeyou.mapper.VoiceNotifyMapper;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.http.HttpUtil;
import com.rm.zeyou.voice.service.NotifyService;
import com.rm.zeyou.voice.vo.response.MiddleNumberAXBNotifyResponseVo;
import com.rm.zeyou.voice.vo.response.VoiceNotifyResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author 🤙🤙🏼🤙🏽
 * @date 2025 4月 08 11:28
 * @Description
 */
@Slf4j
@Service
public class NotifyServiceImpl implements NotifyService {
    @Resource
    private VoiceNotifyMapper voiceNotifyMapper;
    @Resource
    private MiddleNotifyMapper middleNotifyMapper;

    @Override
    public String voiceNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("语音通知回调");
        String s = HttpUtil.readData(request);
        log.info("解析获取的参数：{}", JSON.toJSONString(s));
        VoiceNotifyResponseVo voiceNotifyResponseVo = JSONObject.parseObject(s, VoiceNotifyResponseVo.class);
        log.info("转换实体后获取的参数：{}", JSON.toJSONString(voiceNotifyResponseVo));

        VoiceNotify voiceNotify = voiceNotifyMapper.selectOne(new LambdaQueryWrapper<VoiceNotify>()
                .eq(VoiceNotify::getCallId, voiceNotifyResponseVo.getCallId()));
        if (!CheckEmptyUtil.isEmpty(voiceNotify)){
            voiceNotify.setStartCallTime(voiceNotifyResponseVo.getStartCallTime());
            voiceNotify.setAnsweredTime(voiceNotifyResponseVo.getAnsweredTime());
            voiceNotify.setEndTime(voiceNotifyResponseVo.getEndTime());
            voiceNotify.setCallState(voiceNotifyResponseVo.getCallState());
            voiceNotify.setRecUrl(voiceNotifyResponseVo.getRecUrl());
            voiceNotifyMapper.updateById(voiceNotify);
        }

        HashMap<String, String> map = new HashMap<>(2);
        map.put("resultCode","200");
        return JSON.toJSONString(map);
    }

    @Override
    public String middleNumberAXBNotify(HttpServletRequest request, HttpServletResponse response) {
        log.info("绑定小号语音通知回调");
        String s = HttpUtil.readData(request);
        log.info("解析获取的参数：{}", JSON.toJSONString(s));
        MiddleNumberAXBNotifyResponseVo responseVo = JSONObject.parseObject(s, MiddleNumberAXBNotifyResponseVo.class);
        log.info("转换实体后获取的参数：{}", JSON.toJSONString(responseVo));
        MiddleNotify middleNotify = middleNotifyMapper.selectOne(new LambdaQueryWrapper<MiddleNotify>()
                .eq(MiddleNotify::getBindId, responseVo.getBindId()));
        if (!CheckEmptyUtil.isEmpty(middleNotify)){
            middleNotify.setCallId(responseVo.getCallId());
            middleNotify.setCallerNumber(responseVo.getCallerNumber());
            middleNotify.setCalleeNumber(responseVo.getCalleeNumber());
            middleNotify.setStartCallTime(responseVo.getStartCallTime());
            middleNotify.setCallerAnsweredTime(responseVo.getCallerAnsweredTime());
            middleNotify.setCalleeAnsweredTime(responseVo.getCalleeAnsweredTime());
            middleNotify.setEndTime(responseVo.getEndTime());
            middleNotify.setCallerState(responseVo.getCallerState());
            middleNotify.setCalleeState(responseVo.getCalleeState());
            middleNotify.setCallerDuration(responseVo.getCallerDuration());
            middleNotify.setCalleeDuration(responseVo.getCalleeDuration());
            middleNotify.setDuration(responseVo.getDuration());
            if (CheckEmptyUtil.isEmpty(middleNotify.getRecUrl())){
                middleNotify.setRecUrl(responseVo.getRecUrl());
            }else {
                middleNotify.setRecUrl(middleNotify.getRecUrl()+","+responseVo.getRecUrl());
            }

            middleNotifyMapper.updateById(middleNotify);
        }
        HashMap<String, String> map = new HashMap<>(2);
        map.put("resultCode","200");
        return JSON.toJSONString(map);
    }
}
